package com.bootdo.system.service.impl;

import com.bootdo.common.exception.DlException;
import com.bootdo.common.exception.ExceptionEnum;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UploadType;
import com.bootdo.system.service.UpLoadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.UUID;

/**
 * @author: 况鑫威
 * @Date: 2021/4/18
 * @Time: 14:35
 * @ClassName: UpLoadServiceImpl
 * @Description: com.ncxhr.logic.service.iml
 */
@Service
public class UpLoadServiceImpl implements UpLoadService {

    @Value("${upload.IP}")
    private String IP;
    @Value("${upload.AttachedResume}")
    private String attachedResume;
    @Value("${upload.PhotoAlbum}")
    private String photoAlbum;
    @Value("${upload.Avatar}")
    private String avatar;
    @Value("${upload.IdentitycardFront}")
    private String identitycardFront;
    @Value("${upload.IdentitycardContrary}")
    private String identitycardContrary;
    @Value("${upload.IdentitycardHand}")
    private String identitycardHand;
    @Value("${upload.BusinessLicense}")
    private String businessLicense;
    @Value("${upload.Companylog}")
    private String companylog;
    @Value("${upload.bannerPhoto}")
    private String bannerPhoto;
    @Value("${upload.titlePhoto}")
    private String titlePhoto;

    /**
     * 功能描述: 上传文件
     * @Param: [file, type]
     * @Return: com.dlit.logic.vo.ResultInfo
     * @Author: 况鑫威
     * @Date: 2021/4/26
     * @Time: 14:56
     *
     * 相册 = "1";
     * 附件简历 = "2";
     * 身份证正面 = "3";
     * 头像 = "4";
     * 身份证反面= "5";
     * 手持身份证 = "6";
     * 营业执照 = "7";
     * 公司log = "8";
     */
    @Override
    public R upLoad(MultipartFile file, String type) {
        //上传文件的实体路径
        String realUploadPath = null;
        //文件存储的路径
            //word
        if(UploadType.ATTACHED_RESUME.equals(type)){
            realUploadPath = attachedResume;
            //相册
        } else if(UploadType.PHOTO_ALBUM.equals(type)){
            realUploadPath = photoAlbum;
            //头像
        }else if(UploadType.AVATAR.equals(type)){
            realUploadPath = avatar;
            //法人身份证正面
        }else if(UploadType.IDENTITYCARDFRONT.equals(type)){
            realUploadPath = identitycardFront;
            //法人身份证反面
        }else if(UploadType.IDENTITYCARDCONTRARY.equals(type)){
            realUploadPath = identitycardContrary;
            //法人手持身份证
        }else if(UploadType.IDENTITYCARDHAND.equals(type)){
            realUploadPath = identitycardHand;
            //营业执照图片
        }else if(UploadType.BUSINESSLICENSE.equals(type)){
            realUploadPath = businessLicense;
            //banner图
        }else if(UploadType.BANNERPHOTO.equals(type)){
            realUploadPath = bannerPhoto;
            //标题图
        }else if(UploadType.TITLEPHOTO.equals(type)){
            realUploadPath = titlePhoto;
            //公司log
        }else if(UploadType.COMPANYLOGO.equals(type)){
            realUploadPath = companylog;
        }else {
            return R.error("不正确的类型");
        }
        // 文件名
        String fileName = file.getOriginalFilename();
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //新文件名
        String newFileName = UUID.randomUUID() + suffixName;
        //创建File
        try {
            FileUtil.uploadFile(file.getBytes(), realUploadPath, newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DlException(ExceptionEnum.FILE_SENDING_FAILED);
        }
        //返回两个名字
        HashMap<String, Object> map = new HashMap<>();
        map.put("fileName", fileName);
        map.put("newFileName", newFileName);
        map.put("path", IP + "/upload/" + newFileName);
        //创建返回的信息
        return R.ok(map);
    }
}
